package com.admore.demo.banner;

import static com.admore.sdk.config.AdMoreConstants.AD_COUNT;
import static com.admore.sdk.config.AdMoreConstants.AD_HEIGHT;
import static com.admore.sdk.config.AdMoreConstants.AD_WIDTH;

import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.admore.demo.App;
import com.admore.demo.R;
import com.admore.demo.util.UIUtils;
import com.admore.sdk.ads.AdMoreBannerAd;
import com.admore.sdk.ads.BannerAd;
import com.admore.sdk.callback.AdMoreDislikeListener;
import com.admore.sdk.callback.AdMoreLoadListener;
import com.admore.sdk.callback.AdMoreMediaListener;
import com.admore.sdk.sdk.AdMoreSdk;
import com.zcy.base.ui.BaseActivity;

import java.util.HashMap;

public class BannerActivity extends BaseActivity {

    private ViewGroup adMain;
    private AdMoreBannerAd adMoreBannerAd;

    @Override
    public int bindLayout() {
        return R.layout.activity_banner;
    }

    @Override
    public void initView() {
        adMain = findViewById(R.id.ad_main);
        ((TextView) findViewById(R.id.title)).setText("banner");
        ((TextView) findViewById(R.id.title)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (adMoreBannerAd == null) {
                    startLoadBanner();
                } else {
                    if (adMoreBannerAd.getBannerAdList().size() > 0) {
                        showBanner();
                    } else {
                        adMoreBannerAd.loadAd();
                    }
                }
            }
        });

    }

    @Override
    public void initData() {
        startLoadBanner();
    }

    private void startLoadBanner() {
        if (!AdMoreSdk.init) {
            return;
        }
        Log.d(TAG, "startLoadBanner ");
        adMain.removeAllViews();
        if (adMoreBannerAd == null) {
            HashMap<String, Object> options = new HashMap<>();

            options.put(AD_WIDTH, UIUtils.getScreenWidth(getApplicationContext()));
            options.put(AD_HEIGHT, UIUtils.getScreenHeight(getApplicationContext()));
            adMoreBannerAd = AdMoreSdk.getAdMoreSdkManager().createBanner(this, App.AD_MORE_BANNER_ID, options);
            adMoreBannerAd.setLoadListener(new AdMoreLoadListener() {
                @Override
                public void onAdLoadSuccess(String msg) {
                    Log.d(TAG, "onAdLoadSuccess ");
                    showBanner();
                }

                @Override
                public void onAdLoadError(Throwable throwable) {
                    Log.d(TAG, "onAdLoadError " + throwable);
                }
            });
        }
        adMoreBannerAd.loadAd();
    }

    public void showBanner() {
        // 加载成功才能展示
        if (adMoreBannerAd == null || adMoreBannerAd.getBannerAdList().size() == 0) {
            Toast.makeText(getApplicationContext(), "请先加载广告", Toast.LENGTH_SHORT).show();
            return;
        }
        adMain.removeAllViews();
        final BannerAd bannerAd = adMoreBannerAd.getBannerAdList().remove(0);
        bannerAd.setMediaListener(new AdMoreMediaListener() {
            @Override
            public void onAdClosed() {
                Log.d(TAG, "onAdClosed");
            }

            @Override
            public void onAdShow() {
                Log.d(TAG, "onAdShow ecpm:" + bannerAd.getAdMoreInfo().getECPM());
            }
        });
        bannerAd.setAdMoreDislikeListener(new AdMoreDislikeListener() {

            @Override
            public void onSelected(boolean closeFlag) {
                Log.d(TAG, "onSelected " + closeFlag);
            }

            @Override
            public void onCancel() {
                Log.d(TAG, "onCancel ");
            }
        });
        bannerAd.render(this, adMain);
    }
}
